package me.caofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import me.caofeng.dao.CategoryMapper;
import me.caofeng.dao.ProductMapper;
import me.caofeng.enums.ItemStatus;
import me.caofeng.enums.ResponseCode;
import me.caofeng.enums.Role;
import me.caofeng.pojo.Page;
import me.caofeng.pojo.Product;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.IProductService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.service
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-20 22:36
 * @Version: V1.0
 */
@Service
@Transactional
public class ProductService implements IProductService {

    private static Logger LOGGER = LogManager.getLogger(ProductService.class);

    private static Cache<Integer, Product> productCache = CacheBuilder
            .newBuilder()
            .expireAfterWrite(2, TimeUnit.HOURS)
            .maximumSize(1000)
            .build();

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public Response<Product> addProduct(Product product) {
        product.setStatus(ItemStatus.EDIT);

        Integer categoryId = categoryMapper.isCategoryExists(product.getCategoryId());
        LOGGER.debug(categoryId);
        if (null == categoryId || categoryId == 0) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "分类不能存在", null);
        }

        int row = productMapper.addProduct(product);

        if (row == 1) {
            return Response.response(ResponseCode.SUCCESS, "添加商品成功", product);
        }

        return Response.response(ResponseCode.ERROR, "保存商品失败", null);
    }

    @Override
    public Response<Product> getProduct(Integer id,Role role) {

        Product product = productMapper.getProduct(id, role.getId());

        return Response.response(ResponseCode.SUCCESS, "获取产品详情成功", product);
    }

    @Override
    public Response<Product> updateProduct(Product product, Integer id) {
        LOGGER.debug(product + " id " + id);
        if (null == product.getId() || id != product.getId()) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "商品不存在", null);
        }

        Integer productId = productMapper.isProductExists(id);
        if (productId != id) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, "商品不存在", null);
        }

        Integer categoryId = categoryMapper.isCategoryExists(product.getCategoryId());
        if (null == categoryId || 0 == categoryId) {
            return Response.response(ResponseCode.ERROR, "商品分类不存在", null);
        }

        int row = productMapper.updateProduct(product);

        if (row != 1) {
            return Response.response(ResponseCode.ERROR, "更新商品信息失败", null);
        }
        productCache.invalidate(id);
        return Response.response(ResponseCode.SUCCESS, "更新成功", product);

    }

    @Override
    public Response updateProductStatus(Integer id, Integer status) {
        ItemStatus itemStatus = ItemStatus.getStatus(status);
        if (null == itemStatus) {
            return Response.response(ResponseCode.ERROR, "修改商品状态失败", null);
        }

        int row = productMapper.updateProductStatus(id, itemStatus);
        if (row != 1) {
            return Response.response(ResponseCode.ERROR, "修改商品状态失败", null);
        }
        return Response.response(ResponseCode.SUCCESS, "修改状态成功", null);
    }

    @Override
    public Response<PageInfo<List<Product>>> getProductsAsPageInfo(
            Integer pageNum, Integer pageSize,
            Integer categoryId, String key, Integer status,
            Role role) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.
                searchProducts(categoryId, key, status, role.getId());
        PageInfo<List<Product>> pageInfo = new PageInfo(products);
        if (products.size() == 0) {
            return Response.response(ResponseCode.EMPTY, "获取商品列表成功", null);
        }
        return Response.response(ResponseCode.SUCCESS, "获取商品列表成功", pageInfo);
    }

    @Override
    public Response<Page<List<Product>>> getProductsAsPage(
            Integer pageNum, Integer pageSize,
            Integer categoryId, String key, Integer status,
            Role role) {
        Page<List<Product>> page = new Page<>(pageNum, pageSize);
        List<Product> products = productMapper
                .searchProductsByPage(categoryId, key, page, status, role.getId());

        if (products.size() == 0) {
            return Response.response(ResponseCode.EMPTY, "获取商品列表成功", null);
        }
        page.setPageNumber(pageNum);
        page.setSize(products.size());
        page.setList(products);
        return Response.response(ResponseCode.SUCCESS, "ok", page);
    }


}
